const Admin = require('../../models/Admin');
const {Op} = require('sequelize');
const bcrypt = require('bcryptjs');
const jwt = require('jsonwebtoken');
const config = require('../../config/config');
const admin = require('../../routes/admin');
const adminLoginMiddleWare = (req,res)=>{
    let {username,password} = req.body;
    Admin.findAll(
        {
            where:{
                name:username
            } 
        }
    )
    .then(values=>{
        if(values.length !==0){
            if(bcrypt.compareSync(password,values[0].dataValues.password)){
                //创建token
                const tokenStr = jwt.sign({ username }, config.jwtSecretKey, {
                    expiresIn: '24h', //token有效期24小时
                });
                res.status(200).send({
                    message: '登录成功',
                    //为了方便客户端的使用token,所以在服务器端直接拼接上'Bearer '(有一个空格)的前缀。
                    token: 'Bearer ' + tokenStr
                });
            }else{
                res.status(422).send({
                    message: "账号密码错误"
                });
            }
        }else{
            res.status(422).send({
                message: "账号密码错误"
            });
        }
        
    })
    .catch(err=>{
        console.log("err ",err);
        res.send('error');
    });
}
const adminAddMiddleWare =(req,res)=>{
    let {username,password} = req.body;
    //加密密码
    password = bcrypt.hashSync(password,8);
    Admin.create({
        name:username,
        password:password
    }).then(values=>{
        console.log("values \n",values);
    }).catch(err=>{
        console.log("err \n",err);
    });
    res.send('success');
}
const adminShowMiddleWare =(req,res)=>{
    Admin.findAll().then(values=>{
        console.log("values");
        return values.map(adm=>{
            return adm.dataValues;
        })
    }).then(val=>{
        let adminsArr = [];
        val.forEach(ad=>{
            let adm = {};
            adm.id = ad.id;
            adm.username = ad.name;
            adminsArr.push(adm);
        });
        res.status(200).send({
            adminsList:adminsArr
        });
    })
    .catch(err=>{
        console.log("没找到全部的管理员 ",err);
    });
}
const deleteAdminByIdMiddleWare = (req,res)=>{
    let {id} = req.params;
    Admin.destroy({
        where:{
            id
        }
    }).then(val=>{
        console.log("val ",val);
    }).catch(err=>{
        console.log("err ",err);
    });
    res.send('success');
}
const selectAdminByIdMiddleWare = (req,res)=>{
    let {id} = req.params;
    Admin.findAll({
        where:{
            id
        }
    }).then(val=>{
        let name = val[0].dataValues.name;
        res.send({name});
    }).catch(err=>{
        console.log(err);
        res.send('err');
    });
}
const modifyAdminByIdMiddleWare = (req,res)=>{
    let {id,username,password} = req.body;
    Admin.update({name:username,password:bcrypt.hashSync(password,8)},{
        where:{
            id
        }
    }).then(val=>{
        console.log("更新成功 ",val);
    }).catch(err=>{
        console.log("更新失败 ",err);
    });
    res.send('success');
}
module.exports = {
    adminLoginMiddleWare,
    adminAddMiddleWare,
    adminShowMiddleWare,
    deleteAdminByIdMiddleWare,
    selectAdminByIdMiddleWare,
    modifyAdminByIdMiddleWare
}